package net.lzzy.practices.web.controller;

import io.swagger.annotations.Api;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Applicant;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ApplicantService;
import net.lzzy.practices.utils.StringUtil;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.IllegalFormatException;
import java.util.List;
import java.util.UUID;

/**
 * @author m
 */
@RestController

@RequestMapping(ApiConstant.ROUTE_APPLY_ROOT)
@CrossOrigin
@Api(tags = "申请管理")
public class ApplicantApiController {
    
    private final ApplicantService service;
@Autowired
    public ApplicantApiController(ApplicantService service) {
        this.service = service;
    }

    /**
     * 申请学习课程
     * @param info
     * @return
     */
    @PostMapping(ApiConstant.ROUTE_APPLY_APPLY)
    public ApiResult apply(@RequestBody ApplicantInfo info){
        String token=info.getToken();
        String strStudentId= info.getStudentId();
        String strCourseId=info.getCourseId();
        
       
        
        if( StringUtil.isEmpty(strStudentId) || StringUtil.isEmpty(strCourseId) || StringUtil.isEmpty(token))
        {
            return  new ApiResult(false, Messages.MISS_ARGS.toString(),null);
        }
        try{
            UUID studentId=UUID.fromString(strStudentId);
            UUID courseId=UUID.fromString(strCourseId);
            Applicant applicant= service.apply(studentId,courseId,token);
            if(applicant==null){
                return new ApiResult(false,Messages.WRONG_ID.toString(),null);
                
            }
            return  new ApiResult(true,Messages.SUCCESC.toString(),new VmApplicant(applicant));
            
        }catch (IllegalFormatException e){
            return  new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
    }
    
    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_COURSES)
    public ApiResult getStudentApplicants(@RequestParam(name = "id") UUID studentId,@RequestParam String token){
        List<Applicant> applicants=service.getAllCourses(studentId,token);
        return transform(applicants);


    }

    private static ApiResult transform(List<Applicant> applicants) {
        if(applicants == null){
            return new ApiResult(false, Messages.NO_PERISSION.toString(), null);
        }

        List<VmApplicant> vmApplicants= new ArrayList<>();
        applicants.forEach(applicant -> vmApplicants.add(new VmApplicant(applicant)));
        return new ApiResult(true, Messages.SUCCESC.toString(), vmApplicants);
    }

    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_ENROLLED)
    public  ApiResult getEnrolledApplicants(@RequestParam(name = "id") UUID studentId,@RequestParam String token){
        List<Applicant> applicants= service.getEnrolledCourses(studentId,token);
        return  transform(applicants);
    }

    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_APPLYING)
    public  ApiResult getApplyingApplicant(@RequestParam(name = "id") UUID studentId,@RequestParam String token){
        List<Applicant> applicants= service.getApplyingCourses(studentId,token);
        return  transform(applicants);
    }
    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_DECLINED)
    public  ApiResult getDeclineApplicant(@RequestParam(name = "id") UUID studentId,@RequestParam String token){
        List<Applicant> applicants= service.getDeclinedCourses(studentId,token);
        return  transform(applicants);
    }

    @PostMapping(ApiConstant.ROUTE_APPLY_APPROVE)
    public  ApiResult approve(@RequestBody ApproveInfo info){
        String token=info.getToken();
        String strApplicantId=info.getApplicantId();
        if( StringUtil.isEmpty(strApplicantId)  || StringUtil.isEmpty(token))
        {
            return  new ApiResult(false, Messages.MISS_ARGS.toString(),null);
        }
        try{
            UUID applicantId=UUID.fromString(strApplicantId);
            Applicant applicant= service.approve(applicantId,token);
            if(applicant==null){
                return new ApiResult(false,Messages.WRONG_ID.toString(),null);

            }
            return  new ApiResult(true,Messages.SUCCESC.toString(),new VmApplicant(applicant));

        }catch (IllegalFormatException e){
            return  new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
    }
    @PostMapping(ApiConstant.ROUTE_APPLY_DECLINE)
    public  ApiResult decline(@RequestBody ApproveInfo info){
        String token=info.getToken();
        String strApplicantId=info.getApplicantId();
        if( StringUtil.isEmpty(strApplicantId)  || StringUtil.isEmpty(token))
        {
            return  new ApiResult(false, Messages.MISS_ARGS.toString(),null);
        }
        try{
            UUID applicantId=UUID.fromString(strApplicantId);
            Applicant applicant= service.decline(applicantId,token);
            if(applicant==null){
                return new ApiResult(false,Messages.WRONG_ID.toString(),null);

            }
            return  new ApiResult(true,Messages.SUCCESC.toString(),new VmApplicant(applicant));

        }catch (IllegalFormatException e){
            return  new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }

    }

    @GetMapping(ApiConstant.ROUTE_APPLY_TEACHER_APPLYING)
    public  ApiResult getApplyingStudent(@RequestParam(name = "id") UUID courseId,@RequestParam String token){
        List<Applicant> applicants= service.getApplyingStudents(courseId,token);
        return  transform(applicants);
    }

    @GetMapping(ApiConstant.ROUTE_APPLY_TEACHER_ENROLLED)
    public  ApiResult getEnrolledStudents(@RequestParam(name = "id") UUID courseId,@RequestParam String token){
        List<Applicant> applicants= service.getEnrolledStudents(courseId,token);
        return  transform(applicants);
    }

    @GetMapping(ApiConstant.ROUTE_APPLY_TEACHER_DECLINED)
    public  ApiResult getDeclinedStudents(@RequestParam(name = "id") UUID courseId,@RequestParam String token){
        List<Applicant> applicants= service.getEnrolledStudents(courseId,token);
        return  transform(applicants);
    }

    @PostMapping(ApiConstant.ROUTE_APPLY_REMOVE)
    public ApiResult remove(@RequestBody ApplicantInfo info ){
        String token= info.getToken();
        String strCourseId=info.getCourseId();
        String strStudentId=info.getStudentId();
        if( StringUtil.isEmpty(strStudentId) || StringUtil.isEmpty(strCourseId) || StringUtil.isEmpty(token))
        {
            return  new ApiResult(false, Messages.MISS_ARGS.toString(),null);
        }
        try{
            UUID courseId= UUID.fromString(strCourseId);
            UUID studentId=UUID.fromString(strStudentId);
            Course course = service.removeStudent(courseId,studentId,token);
            if(course==null){
                return  new ApiResult(false,Messages.NO_PERISSION.toString(),null);
            }
            return  new ApiResult(true,Messages.SUCCESC.toString(), VmCourse.create(course,true));

        }catch (IllegalFormatException e){
            return  new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }


    }





}
